getwd()
## [1] "/Users/kyeongjekim/Documents/GitHub/Study_2/viz"
setwd("/Users/kyeongjekim/Documents/GitHub/Study_2")
library(ggplot2)
library(devtools)
## Loading required package: usethis
library(leaflet)
library(plotly)
##
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
##
## last_plot
## The following object is masked from 'package:stats':
##
## filter
## The following object is masked from 'package:graphics':
##
## layout
library(highcharter)
## Registered S3 method overwritten by 'quantmod':
## method from
## as.zoo.data.frame zoo
library(sf)
## Linking to GEOS 3.8.1, GDAL 3.1.4, PROJ 6.3.1
library(raster)
## Loading required package: sp
##
## Attaching package: 'raster'
## The following object is masked from 'package:plotly':
##
## select
library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:raster':
##
## intersect, select, union
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
library(spData)
## To access larger datasets in this package, install the spDataLarge
## package with: `install.packages('spDataLarge',
## repos='https://nowosad.github.io/drat/', type='source')`
library(tmap)
library(cartogram)
library(RColorBrewer)
library(tigris)
## To enable
## caching of data, set `options(tigris_use_cache = TRUE)` in your R script or .Rprofile.
library(sp)
library(ggmap)
## Google's Terms of Service: https://cloud.google.com/maps-platform/terms/.
## Please cite ggmap if you use it! See citation("ggmap") for details.
##
## Attaching package: 'ggmap'
## The following object is masked from 'package:plotly':
##
## wind
library(maptools)
## Checking rgeos availability: TRUE
library(broom)
library(httr)
##
## Attaching package: 'httr'
## The following object is masked from 'package:plotly':
##
## config
library(rgdal)
## rgdal: version: 1.5-23, (SVN revision 1121)
## Geospatial Data Abstraction Library extensions to R successfully loaded
## Loaded GDAL runtime: GDAL 3.1.4, released 2020/10/20
## Path to GDAL shared files: /Library/Frameworks/R.framework/Versions/4.0/Resources/library/rgdal/gdal
## GDAL binary built with GEOS: TRUE
## Loaded PROJ runtime: Rel. 6.3.1, February 10th, 2020, [PJ_VERSION: 631]
## Path to PROJ shared files: /Library/Frameworks/R.framework/Versions/4.0/Resources/library/rgdal/proj
## Linking to sp version:1.4-5
## To mute warnings of possible GDAL/OSR exportToProj4() degradation,
## use options("rgdal_show_exportToProj4_warnings"="none") before loading rgdal.
library(mapcan)
library(highcharter)
library(DT)
library(htmlwidgets)
# Data Source: http://insideairbnb.com/get-the-data.html
# Please feel free to look around the website if you want to add more cities.
# import datset and check head
gs19 <- read.csv("/Users/kyeongjekim/Desktop/Columbia S2021/Data_Visualization/tP/gs19.csv")
df20 <- read.csv("/Users/kyeongjekim/Desktop/Columbia S2021/Data_Visualization/tP/total_20.csv")
#gs19 <- df19 %>%
# select(id, scrape_id, last_scraped, name, description, host_since, host_location, host_neighbourhood, neighbourhood, neighbourhood_cleansed, neighbourhood_group_cleansed, latitude, latitude, longitude, property_type, room_type, accommodates, bathrooms, bedrooms, beds, amenities, price, minimum_nights, maximum_nights, last_review, review_scores_rating, review_scores_accuracy, review_scores_communication, review_scores_location, review_scores_value, reviews_per_month)
#write.csv(gs19, "gs19.csv")
gs20 <- df20
pal <- colorFactor(c("navy", "red", "gold", "green"), domain = c("Entire home/apt", "Private room", "Hotel room", "Shared room"))
g1 <- gs19 %>%
select(latitude, longitude, room_type, property_type, neighbourhood_group_cleansed, price)
g2 <- gs20 %>%
select(latitude, longitude, room_type, property_type, neighbourhood_group_cleansed, price)
r <- GET('http://data.beta.nyc//dataset/0ff93d2d-90ba-457c-9f7e-39e47bf2ac5f/resource/35dd04fb-81b3-479b-a074-a27a37888ce7/download/d085e2f8d0b54d4590b1e7d1f35594c1pediacitiesnycneighborhoods.geojson')
nyc_neighborhoods <- readOGR(content(r,'text'), 'OGRGeoJSON', verbose = F)
## No encoding supplied: defaulting to UTF-8.
nyc_neighborhoods_df <- tidy(nyc_neighborhoods)
## Regions defined for each Polygons
set.seed(2021)
lats <- g1$latitude
lngs <- g1$longitude
points <- data.frame(lat=lats, lng=lngs)
points_spdf <- points
coordinates(points_spdf) <- ~lng + lat
proj4string(points_spdf) <- proj4string(nyc_neighborhoods)
## Warning in proj4string(nyc_neighborhoods): CRS object has comment, which is lost
## in output
matches <- over(points_spdf, nyc_neighborhoods)
points <- cbind(points, matches)
points_by_neighborhood <- points %>%
group_by(neighborhood) %>%
summarize(num_airbnb=n())
map_data <- geo_join(nyc_neighborhoods, points_by_neighborhood, "neighborhood", "neighborhood")
pal <- colorNumeric(palette = "RdBu",
domain = range(map_data@data$num_airbnb, na.rm=T))
plot_data <- tidy(nyc_neighborhoods, region="neighborhood") %>%
left_join(., points_by_neighborhood, by=c("id"="neighborhood")) %>%
filter(!is.na(num_airbnb))
## Warning in RGEOSUnaryPredFunc(spgeom, byid, "rgeos_isvalid"): Self-intersection
## at or near point -73.998769409999994 40.671602049999997
## SpP is invalid
## Warning in rgeos::gUnaryUnion(spgeom = SpP, id = IDs): Invalid objects found;
## consider using set_RGEOS_CheckValidity(2L)
library(ggmap)
manhattan_map <- get_map(location = c(lon = -74.00, lat = 40.77), maptype = "terrain", zoom = 10)
## Source : https://maps.googleapis.com/maps/api/staticmap?center=40.77,-74&zoom=10&size=640x640&scale=2&maptype=terrain&language=en-EN&key=xxx
num_airbnb <- ggmap(manhattan_map) +
geom_polygon(data=plot_data, aes(x=long, y=lat, group=group, fill=num_airbnb), alpha=0.75) +
labs(
title = "Number of Airbnb by borough in NYC in 2019"
)
plotly_num_airbnb19 <- plotly::ggplotly(num_airbnb)
plotly_num_airbnb19
set.seed(2022)
lats2 <- g2$latitude
lngs2 <- g2$longitude
points2 <- data.frame(lat=lats2, lng=lngs2)
points_spdf2 <- points2
coordinates(points_spdf2) <- ~lng + lat
proj4string(points_spdf2) <- proj4string(nyc_neighborhoods)
## Warning in proj4string(nyc_neighborhoods): CRS object has comment, which is lost
## in output
matches2 <- over(points_spdf2, nyc_neighborhoods)
points2 <- cbind(points2, matches2)
points_by_neighborhood2 <- points2 %>%
group_by(neighborhood) %>%
summarize(num_airbnb=n())
map_data2 <- geo_join(nyc_neighborhoods, points_by_neighborhood2, "neighborhood", "neighborhood")
pal <- colorNumeric(palette = "RdBu",
domain = range(map_data2@data$num_airbnb, na.rm=T))
plot_data2 <- tidy(nyc_neighborhoods, region="neighborhood") %>%
left_join(., points_by_neighborhood2, by=c("id"="neighborhood")) %>%
filter(!is.na(num_airbnb))
## Warning in RGEOSUnaryPredFunc(spgeom, byid, "rgeos_isvalid"): Self-intersection
## at or near point -73.998769409999994 40.671602049999997
## SpP is invalid
## Warning in rgeos::gUnaryUnion(spgeom = SpP, id = IDs): Invalid objects found;
## consider using set_RGEOS_CheckValidity(2L)
library(ggmap)
manhattan_map <- get_map(location = c(lon = -74.00, lat = 40.77), maptype = "terrain", zoom = 10)
## Source : https://maps.googleapis.com/maps/api/staticmap?center=40.77,-74&zoom=10&size=640x640&scale=2&maptype=terrain&language=en-EN&key=xxx
num_airbnb20 <- ggmap(manhattan_map) +
geom_polygon(data=plot_data2, aes(x=long, y=lat, group=group, fill=num_airbnb), alpha=0.75) +
labs(
title = "Number of Airbnb by borough in NYC in 2020"
)
plotly_num_airbnb20 <- plotly::ggplotly(num_airbnb20)
plotly_num_airbnb20